home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 July: Mac OS SDK / Dev.CD Jul 97 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / Interfaces / PInterfaces / DigitalSignature.p < prev    next >
Encoding:
Text File  |  1995-07-06  |  16.3 KB  |  414 lines  |  [TEXT/MPS ]

  1. {
  2.      File:        DigitalSignature.p
  3.  
  4.      Contains:    Digital Signature Interfaces.
  5.  
  6.      Version:    Technology:    AOCE toolbox 1.02
  7.                  Package:    Universal Interfaces 2.1 in “MPW Latest” on ETO #18
  8.  
  9.      Copyright:    © 1984-1995 by Apple Computer, Inc.
  10.                  All rights reserved.
  11.  
  12.      Bugs?:        If you find a problem with this file, use the Apple Bug Reporter
  13.                  stack.  Include the file and version information (from above)
  14.                  in the problem description and send to:
  15.                      Internet:    apple.bugs@applelink.apple.com
  16.                      AppleLink:    APPLE.BUGS
  17.  
  18. }
  19.  
  20. {$IFC UNDEFINED UsingIncludes}
  21. {$SETC UsingIncludes := 0}
  22. {$ENDC}
  23.  
  24. {$IFC NOT UsingIncludes}
  25.  UNIT DigitalSignature;
  26.  INTERFACE
  27. {$ENDC}
  28.  
  29. {$IFC UNDEFINED __DIGITALSIGNATURE__}
  30. {$SETC __DIGITALSIGNATURE__ := 1}
  31.  
  32. {$I+}
  33. {$SETC DigitalSignatureIncludes := UsingIncludes}
  34. {$SETC UsingIncludes := 1}
  35.  
  36.  
  37. {$IFC UNDEFINED __TYPES__}
  38. {$I Types.p}
  39. {$ENDC}
  40. {    ConditionalMacros.p                                            }
  41.  
  42. {$IFC UNDEFINED __MEMORY__}
  43. {$I Memory.p}
  44. {$ENDC}
  45. {    MixedMode.p                                                    }
  46.  
  47. {$IFC UNDEFINED __FILES__}
  48. {$I Files.p}
  49. {$ENDC}
  50. {    OSUtils.p                                                    }
  51. {    Finder.p                                                    }
  52.  
  53. {$PUSH}
  54. {$ALIGN MAC68K}
  55. {$LibExport+}
  56.  
  57. CONST
  58.     kSIGNewContext                = 1900;
  59.     kSIGDisposeContext            = 1901;
  60.     kSIGSignPrepare                = 1902;
  61.     kSIGSign                    = 1903;
  62.     kSIGVerifyPrepare            = 1904;
  63.     kSIGVerify                    = 1905;
  64.     kSIGDigestPrepare            = 1906;
  65.     kSIGDigest                    = 1907;
  66.     kSIGProcessData                = 1908;
  67.     kSIGShowSigner                = 1909;
  68.     kSIGGetSignerInfo            = 1910;
  69.     kSIGGetCertInfo                = 1911;
  70.     kSIGGetCertNameAttributes    = 1912;
  71.     kSIGGetCertIssuerNameAttributes = 1913;
  72.     kSIGFileIsSigned            = 2500;
  73.     kSIGSignFile                = 2501;
  74.     kSIGVerifyFile                = 2502;
  75.  
  76.     kSIGCountryCode                = 0;
  77.     kSIGOrganization            = 1;
  78.     kSIGStreetAddress            = 2;
  79.     kSIGState                    = 3;
  80.     kSIGLocality                = 4;
  81.     kSIGCommonName                = 5;
  82.     kSIGTitle                    = 6;
  83.     kSIGOrganizationUnit        = 7;
  84.     kSIGPostalCode                = 8;
  85.  
  86.     
  87. TYPE
  88.     SIGNameAttributeType = INTEGER;
  89.  
  90. Certificate status codes returned in SIGCertInfo or SIGSignerInfo from
  91. either SIGGetCertInfo or SIGGetSignerInfo respectively. kSIGValid means that
  92. the certificate is currently valid. kSIGPending means the certificate is
  93. currently not valid - but will be.  kSIGExpired means the certificate has
  94. expired. A time is always associated with a SIGCertStatus.  In each case the
  95. time has a specific interpretation.  When the status is kSIGValid the time is
  96. when the certificate will expire. When the status is kSIGPending the time is
  97. when the certificate will become valid. When the status is kSIGExpired the time
  98. is when the certificate expired. In the SIGCertInfo structure, the startDate
  99. and endDate fields hold the appropriate date information.  In the SIGSignerInfo
  100. structure, this information is provided in the certSetStatusTime field. In the
  101. SIGSignerInfo struct, the status time is actually represented by the SIGSignatureStatus
  102. field which can contain any of the types below. NOTE: The only time you will get 
  103. a kSIGInvalid status is when it pertains to a SIGSignatureStatus field and only when
  104. you get a signature that was created after the certificates expiration date, something
  105. we are not allowing on the Mac but that may not be restricted on other platforms. Also, 
  106. it will not be possible to get a kSIGPending value for SIGSignatureStatus on the Mac but
  107. possibly allowed by other platforms.
  108. }
  109. { Values for SIGCertStatus or SIGSignatureStatus }
  110.  
  111. CONST
  112.     kSIGValid                    = 0;                            { possible for either a SIGCertStatus or SIGSignatureStatus }
  113.     kSIGPending                    = 1;                            { possible for either a SIGCertStatus or SIGSignatureStatus }
  114.     kSIGExpired                    = 2;                            { possible for either a SIGCertStatus or SIGSignatureStatus
  115.     * possible only for a SIGSignatureStatus }
  116.     kSIGInvalid                    = 3;
  117.  
  118.     
  119. TYPE
  120.     SIGCertStatus = INTEGER;
  121.  
  122.     SIGSignatureStatus = INTEGER;
  123.  
  124. { Gestalt selector code - returns toolbox version in low-order word }
  125.  
  126. CONST
  127.     gestaltDigitalSignatureVersion = 'dsig';
  128.  
  129. { Number of bytes needed for a digest record when using SIGDigest }
  130.     kSIGDigestSize                = 16;
  131.  
  132.     
  133. TYPE
  134.     SIGDigestData = ARRAY [0..kSIGDigestSize-1] OF Byte;
  135.     SIGDigestDataPtr = ^Byte;
  136.  
  137.     SIGCertInfo = RECORD
  138.         startDate:                LONGINT;                                { cert start validity date }
  139.         endDate:                LONGINT;                                { cert end validity date }
  140.         certStatus:                SIGCertStatus;                            { see comment on SIGCertStatus for definition }
  141.         certAttributeCount:        LONGINT;                                { number of name attributes in this cert }
  142.         issuerAttributeCount:    LONGINT;                                { number of name attributes in this certs issuer }
  143.         serialNumber:            Str255;                                    { cert serial number }
  144.     END;
  145.  
  146.     SIGCertInfoPtr = ^SIGCertInfo;
  147.  
  148.     SIGSignerInfo = RECORD
  149.         signingTime:            LONGINT;                                { time of signing }
  150.         certCount:                LONGINT;                                { number of certificates in the cert set }
  151.         certSetStatusTime:        LONGINT;                                { Worst cert status time. See comment on SIGCertStatus for definition }
  152.         signatureStatus:        SIGSignatureStatus;                        { The status of the signature. See comment on SIGCertStatus for definition}
  153.     END;
  154.  
  155.     SIGSignerInfoPtr = ^SIGSignerInfo;
  156.  
  157.     SIGNameAttributesInfo = RECORD
  158.         onNewLevel:                BOOLEAN;
  159.         filler1:                BOOLEAN;
  160.         attributeType:            SIGNameAttributeType;
  161.         attributeScript:        ScriptCode;
  162.         attribute:                Str255;
  163.     END;
  164.  
  165.     SIGNameAttributesInfoPtr = ^SIGNameAttributesInfo;
  166.  
  167.     SIGContextPtr = Ptr;
  168.  
  169.     SIGSignaturePtr = Ptr;
  170.  
  171. {
  172. Certificates are always in order. That is, the signers cert is always 0, the
  173. issuer of the signers cert is always 1 etc… to the number of certificates-1.
  174. You can use this constant for readability in your code.
  175. }
  176.  
  177. CONST
  178.     kSIGSignerCertIndex            = 0;
  179.  
  180. {
  181. Call back procedure supplied by developer, return false to cancel the current
  182. process.
  183. }
  184. TYPE
  185.     SIGStatusProcPtr = ProcPtr;  { FUNCTION SIGStatus: BOOLEAN; }
  186.     SIGStatusUPP = UniversalProcPtr;
  187.  
  188. CONST
  189.     uppSIGStatusProcInfo = $00000010; { FUNCTION : 1 byte result; }
  190.  
  191. FUNCTION NewSIGStatusProc(userRoutine: SIGStatusProcPtr): SIGStatusUPP;
  192.     {$IFC NOT GENERATINGCFM }
  193.     INLINE $2E9F;
  194.     {$ENDC}
  195.  
  196. FUNCTION CallSIGStatusProc(userRoutine: SIGStatusUPP): BOOLEAN;
  197.     {$IFC NOT GENERATINGCFM}
  198.     INLINE $205F, $4E90;
  199.     {$ENDC}
  200. {
  201. Resource id's of standard signature icon suite, all sizes and colors are available.
  202. }
  203.  
  204. CONST
  205.     kSIGSignatureIconResID        = -16800;
  206.     kSIGValidSignatureIconResID    = -16799;
  207.     kSIGInvalidSignatureIconResID = -16798;
  208.  
  209. { ——————————————————————————————— CONTEXT CALLS ——————————————————————————————— 
  210. To use the Digital Signature toolbox you will need a SIGContextPtr.  To create
  211. a SIGContextPtr you simply call SIGNewContext and it will create and initialize
  212. a context for you.  To free the memory occupied by the context and invalidate
  213. its internal data, call SIGDisposeContext. An initialized context has no notion
  214. of the type of operation it will be performing however, once you call
  215. SIGSignPrepare SIGVerifyPrepare, or SIGDigestPrepare, the contexts operation
  216. type is set and to switch  to another type of operation will require creating a
  217. new context. Be sure to pass the same context to corresponding toolbox calls
  218. (ie SIGSignPrepare, SIGProcessData, SIGSign)  in other words mixing lets say
  219. signing and verify calls with the same context is not allowed.
  220. }
  221.  
  222. FUNCTION SIGNewContext(VAR context: SIGContextPtr): OSErr;
  223.     {$IFC NOT GENERATINGCFM}
  224.     INLINE $203C, 2, 1900, $AA5D;
  225.     {$ENDC}
  226. FUNCTION SIGDisposeContext(context: SIGContextPtr): OSErr;
  227.     {$IFC NOT GENERATINGCFM}
  228.     INLINE $203C, 2, 1901, $AA5D;
  229.     {$ENDC}
  230. { ——————————————————————————————— SIGNING CALLS ——————————————————————————————— 
  231. Once you have created a SIGContextPtr, you create a signature by calling
  232. SIGSignPrepare once, followed by n calls to SIGProcessData, followed by one call
  233. toRcpt SIGSign. To create another signature on different data but for the same
  234. signer, don't dispose of the context and call SIGProcessData for the new data
  235. followed by a call SIGSign again. In this case the signer will not be prompted
  236. for their signer and password again as it was already provided.  Once you call
  237. SIGDisposeContext, all signer information will be cleared out of the context and
  238. the signer will be re-prompted.  The signer file FSSpecPtr should be set to nil
  239. if you want the toolbox to use the last signer by default or prompt for a signer
  240. if none exists.  The prompt parameter can be used to pass a string to be displayed
  241. in the dialog that prompts the user for their password.  If the substring "^1"
  242. (without the quotes) is in the prompt string, then the toolbox will replace it
  243. with the name of the signer from the signer selected by the user.  If an empty
  244. string is passed, the following default string will be sent to the toolbox
  245. "\pSigning as ^1.".  You can call any of the utility routines after SIGSignPrepare
  246. or SIGSign to get information about the signer or certs.
  247. }
  248. FUNCTION SIGSignPrepare(context: SIGContextPtr; {CONST}VAR signerFile: FSSpec; prompt: ConstStr255Param; VAR signatureSize: Size): OSErr;
  249.     {$IFC NOT GENERATINGCFM}
  250.     INLINE $203C, 8, 1902, $AA5D;
  251.     {$ENDC}
  252. FUNCTION SIGSign(context: SIGContextPtr; signature: SIGSignaturePtr; statusProc: SIGStatusUPP): OSErr;
  253.     {$IFC NOT GENERATINGCFM}
  254.     INLINE $203C, 6, 1903, $AA5D;
  255.     {$ENDC}
  256. { ——————————————————————————————— VERIFYING CALLS ——————————————————————————————— 
  257. Once you have created a SIGContextPtr, you verify a signature by calling
  258. SIGVerifyPrepare  once, followed by n calls to SIGProcessData, followed by one
  259. call to SIGVerify. Check the return code from SIGVerify to see if the signature
  260. verified or not (noErr is returned on  success otherwise the appropriate error
  261. code).  Upon successfull verification, you can call any of the utility routines
  262. toRcpt find out who signed the data.
  263. }
  264. FUNCTION SIGVerifyPrepare(context: SIGContextPtr; signature: SIGSignaturePtr; signatureSize: Size; statusProc: SIGStatusUPP): OSErr;
  265.     {$IFC NOT GENERATINGCFM}
  266.     INLINE $203C, 8, 1904, $AA5D;
  267.     {$ENDC}
  268. FUNCTION SIGVerify(context: SIGContextPtr): OSErr;
  269.     {$IFC NOT GENERATINGCFM}
  270.     INLINE $203C, 2, 1905, $AA5D;
  271.     {$ENDC}
  272. { —————————————————————————————— DIGESTING CALLS —————————————————————————————— 
  273. Once you have created a SIGContextPtr, you create a digest by calling
  274. SIGDigestPrepare once, followed by n calls to SIGProcessData, followed by one
  275. call to SIGDigest.  You can dispose of the context after SIGDigest as the
  276. SIGDigestData does not reference back into it.  SIGDigest returns the digest in
  277. digest.
  278. }
  279. FUNCTION SIGDigestPrepare(context: SIGContextPtr): OSErr;
  280.     {$IFC NOT GENERATINGCFM}
  281.     INLINE $203C, 2, 1906, $AA5D;
  282.     {$ENDC}
  283. FUNCTION SIGDigest(context: SIGContextPtr; VAR digest: SIGDigestData): OSErr;
  284.     {$IFC NOT GENERATINGCFM}
  285.     INLINE $203C, 4, 1907, $AA5D;
  286.     {$ENDC}
  287. { —————————————————————————————— PROCESSING DATA —————————————————————————————— 
  288. To process data during a digest, sign, or verify operation call SIGProcessData
  289. as many times as necessary and with any sized blocks of data.  The data needs to
  290. be processed in the same order during corresponding sign and verify operations
  291. but does not need to be processed in the same sized chunks (i.e., the toolbox
  292. just sees it as a continuous bit stream).
  293. }
  294. FUNCTION SIGProcessData(context: SIGContextPtr; data: UNIV Ptr; dataSize: Size): OSErr;
  295.     {$IFC NOT GENERATINGCFM}
  296.     INLINE $203C, 6, 1908, $AA5D;
  297.     {$ENDC}
  298. { ——————————————————————————————— UTILITY CALLS ——————————————————————————————— 
  299. Given a context that has successfully performed a verification SIGShowSigner
  300. will  display a modal dialog with the entire distinguished name of the person
  301. who signed the data. the prompt (if supplied) will appear at the top of the
  302. dialog.  If no prompt is specified, the default prompt "\pVerification
  303. Successfull." will appear.
  304.  
  305. Given a context that has been populated by calling SIGSignPrepare, SIGSign or a
  306. successful SIGVerify, you can make the remaining utility calls:
  307.  
  308. SIGGetSignerInfo will return the SignerInfo record.  The certCount can be used
  309. toRcpt index into the certificate set when calling SIGGetCertInfo,
  310. SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes. The signingTime is
  311. only defined if the call is made after SIGSign  or SIGVerify. The certSetStatus
  312. will tell you the best status of the entire certificate set while
  313. certSetStatusTime will correspond to the time associated with that status (see
  314. definitions above).
  315.  
  316. SIGGetCertInfo will return the SIGCertInfo record when given a valid index into
  317. the cert set in  certIndex.  Note: The cert at index kSIGSignerCertIndex is
  318. always the signers certificate.  The  serial number, start date and end date
  319. are there should you wish to display that info.  The  certAttributeCount and
  320. issuerAttributeCount provide the number of parts in the name of that certificate
  321. or that certificates issuer respectively.  You use these numbers to index into
  322. either SIGGetCertNameAttributes or SIGGetCertIssuerNameAttributes to retrieve
  323. the name. The certStatus will tell you the status of the certificate while
  324. certStatusTime will correspond to the time associated with that status (see
  325. definitions above).
  326.  
  327. SIGGetCertNameAttributes and SIGGetCertIssuerNameAttributes return name parts
  328. of the certificate at  certIndex and attributeIndex.  The newLevel return value
  329. tells you wether the name attribute returned is at the same level in the name
  330. hierarchy as the previous attribute.  The type return value tells you  the type
  331. of attribute returned. nameAttribute is the actual string containing the name
  332. attribute.   So, if you wanted to display the entire distinguished name of the
  333. person who's signature was just validated you could do something like this;
  334.  
  335.     (…… variable declarations and verification code would preceed this sample ……)
  336.  
  337.     error = SIGGetCertInfo(verifyContext, kSIGSignerCertIndex, &certInfo);
  338.     HandleErr(error);
  339.  
  340.     for (i = 0; i <= certInfo.certAttributeCount-1; i++)
  341.         (
  342.         error = SIGGetCertNameAttributes(
  343.             verifyContext, kSIGSignerCertIndex, i, &newLevel, &type, theAttribute);
  344.         HandleErr(error);
  345.         DisplayNamePart(theAttribute, type, newLevel);
  346.         )
  347. }
  348. FUNCTION SIGShowSigner(context: SIGContextPtr; prompt: ConstStr255Param): OSErr;
  349.     {$IFC NOT GENERATINGCFM}
  350.     INLINE $203C, 4, 1909, $AA5D;
  351.     {$ENDC}
  352. FUNCTION SIGGetSignerInfo(context: SIGContextPtr; VAR signerInfo: SIGSignerInfo): OSErr;
  353.     {$IFC NOT GENERATINGCFM}
  354.     INLINE $203C, 4, 1910, $AA5D;
  355.     {$ENDC}
  356. FUNCTION SIGGetCertInfo(context: SIGContextPtr; certIndex: LONGINT; VAR certInfo: SIGCertInfo): OSErr;
  357.     {$IFC NOT GENERATINGCFM}
  358.     INLINE $203C, 6, 1911, $AA5D;
  359.     {$ENDC}
  360. FUNCTION SIGGetCertNameAttributes(context: SIGContextPtr; certIndex: LONGINT; attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  361.     {$IFC NOT GENERATINGCFM}
  362.     INLINE $203C, 8, 1912, $AA5D;
  363.     {$ENDC}
  364. FUNCTION SIGGetCertIssuerNameAttributes(context: SIGContextPtr; certIndex: LONGINT; attributeIndex: LONGINT; VAR attributeInfo: SIGNameAttributesInfo): OSErr;
  365.     {$IFC NOT GENERATINGCFM}
  366.     INLINE $203C, 8, 1913, $AA5D;
  367.     {$ENDC}
  368. { ——————————————————————————— FILE SIGN & VERIFY CALLS —————————————————————————— 
  369. These calls allow you to detect the presence of a standard signtaure in a file as 
  370. well as sign and verify files in a standard way.  An example of this is the Finder, 
  371. which uses these calls to allow the user to "drop sign" a file.
  372.  
  373. To detect if a file is signed in the standard way, pass the FSSpec of the file to SIGFileIsSigned.
  374. A result of noErr means the file is in fact signed, otherwise, a kSIGNoSignature error will
  375. be returned.
  376.  
  377. Once you have created a SIGContextPtr, you can make calls to either sign or verify a file in
  378. a standard way: 
  379.  
  380. To sign a file, call SIGSignPrepare followed by 'n' number of calls to SIGSignFile,
  381. passing it the file spec for each file you wish to sign in turn.  You supply the context, the signature 
  382. size that was returned from SIGSignPrepare and an optional call back proc.  The call will take care of all
  383. the processing of data and affixing the signature to the file. If a signature already exists in the file, 
  384. it is replaced with the newly created signature.
  385.  
  386. To verify a file that was signed using SIGSignFile, call SIGVerifyFile passing it a new context and 
  387. the file spec.  Once this call has completed, if the verification is successfull, you can pass the context 
  388. to SIGShowSigner to display the name of the person who signed the file.
  389. }
  390. FUNCTION SIGFileIsSigned({CONST}VAR fileSpec: FSSpec): OSErr;
  391.     {$IFC NOT GENERATINGCFM}
  392.     INLINE $203C, 2, 2500, $AA5D;
  393.     {$ENDC}
  394. FUNCTION SIGSignFile(context: SIGContextPtr; signatureSize: Size; {CONST}VAR fileSpec: FSSpec; statusProc: SIGStatusUPP): OSErr;
  395.     {$IFC NOT GENERATINGCFM}
  396.     INLINE $203C, 8, 2501, $AA5D;
  397.     {$ENDC}
  398. FUNCTION SIGVerifyFile(context: SIGContextPtr; {CONST}VAR fileSpec: FSSpec; statusProc: SIGStatusUPP): OSErr;
  399.     {$IFC NOT GENERATINGCFM}
  400.     INLINE $203C, 6, 2502, $AA5D;
  401.     {$ENDC}
  402.  
  403. {$ALIGN RESET}
  404. {$POP}
  405.  
  406. {$SETC UsingIncludes := DigitalSignatureIncludes}
  407.  
  408. {$ENDC} {__DIGITALSIGNATURE__}
  409.  
  410. {$IFC NOT UsingIncludes}
  411.  END.
  412. {$ENDC}
  413.